package condominios.servlets.residente;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import condominios.excepcion.DAOExcepcion;
import condominios.modelo.Persona;
import condominios.negocio.GestionPersonas;


/**
 * Servlet implementation class ResidenteUpdateServlet
 */
@WebServlet("/ResidenteUpdateServlet")
public class ResidenteUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ResidenteUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		System.out.println("Dentro de doPost del servlet ResidenteUpdateSerlvet");
		HttpSession session = request.getSession();
		Persona vo = (Persona)session.getAttribute("user");
				
		if (vo== null){
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
			return;
		}	
		
		String sPersona = "";
		
		try {
			
			GestionPersonas persona1 = new GestionPersonas();
			Collection<Persona> listaFull = persona1.buscarTodos();
            for(Persona x1 : listaFull)            	
            {
            	sPersona = "residente" + x1.getIdPersona();
            	if(request.getParameter(sPersona)!=null)
                {
            		
            		Persona c1 = new Persona();
            		c1.setIdPersona(x1.getIdPersona());
            		c1.setDni(x1.getDni());
            		c1.setNombres(x1.getNombres());
            		c1.setApellidoPaterno(x1.getApellidoPaterno());
            		c1.setApellidoMaterno(x1.getApellidoMaterno());
            		c1.setEmail(x1.getEmail());
            		c1.setIdPerfil(x1.getIdPerfil());
            		            		
            		request.setAttribute("PERSONA_CAMBIAR",c1 );	
                	RequestDispatcher rd1 = request.getRequestDispatcher("residente_modificar.jsp");
            		rd1.forward(request, response);
                	//el codigo de lo que quieres hacer con el boton
                }
            	
            	sPersona = "residented" + x1.getIdPersona();
            	if(request.getParameter(sPersona)!=null)
                {
            		
            		Persona d1 = new Persona();
            		d1.setIdPersona(x1.getIdPersona());
            		d1.setDni(x1.getDni());
            		d1.setNombres(x1.getNombres());
            		d1.setApellidoPaterno(x1.getApellidoPaterno());
            		d1.setApellidoMaterno(x1.getApellidoMaterno());
            		d1.setEmail(x1.getEmail());
            		d1.setIdPerfil(x1.getIdPerfil());
            		
            		            		
            		request.setAttribute("PERSONA_BORRAR",d1 );	
                	RequestDispatcher rd1 = request.getRequestDispatcher("residente_eliminar.jsp");
            		rd1.forward(request, response);
                	//el codigo de lo que quieres hacer con el boton
                }
            }
            
		} catch (DAOExcepcion e) {
			System.out.println(e.getMessage());
			RequestDispatcher rd1 = request.getRequestDispatcher("error.jsp");
			rd1.forward(request, response);
		}   
	}

}
